package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.ResultInfo;
import com.itheima.reggie.domain.Dish;
import com.itheima.reggie.service.DishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class DishController {

    @Autowired
    private DishService dishService;

    @Autowired
    private RedisTemplate redisTemplate;


    //分页查询
    @GetMapping("/dish/page")
    public ResultInfo findByPage(@RequestParam(value = "page",defaultValue = "1") Integer pageNum,
                                 @RequestParam(defaultValue = "10") Integer pageSize,
                                 String name) {

      Page page =  dishService.findByPage(pageNum, pageSize, name);
        return ResultInfo.success(page);
    }

    //根据菜品id查询菜品
    @GetMapping("/dish/{id}")
    public ResultInfo findById(@PathVariable("id") Long id) {
        Dish dish =  dishService.findById(id);
        return ResultInfo.success(dish);
    }


    //保存
    @PostMapping("/dish")
    public ResultInfo save(@RequestBody Dish dish) {
        //清理掉redis中关于菜品(dish_*)的所有缓存
        redisTemplate.delete(redisTemplate.keys("dish_*"));
      dishService.save(dish);
        return ResultInfo.success(null);
    }



    //修改
    @PutMapping("/dish")
    public ResultInfo update(@RequestBody Dish dish) {
        //清理掉redis中关于菜品(dish_*)的所有缓存
        redisTemplate.delete(redisTemplate.keys("dish_*"));

        dishService.update(dish);
        return ResultInfo.success(null);
    }


    //批量删除
    @DeleteMapping("/dish")
    public ResultInfo delete(@RequestParam("ids") List<Long> ids) {
        //清理掉redis中关于菜品(dish_*)的所有缓存
        redisTemplate.delete(redisTemplate.keys("dish_*"));

        dishService.deleteById(ids);
        return ResultInfo.success(null);
    }

    //查询菜品列表
    @GetMapping("/dish/list")
    public ResultInfo findList(Long categoryId,String name) {
      List<Dish> dishList = dishService.findLsit(categoryId,name);
        return ResultInfo.success(dishList);
    }



}
